package com.sinodata.bsm.cicp.finder.sco;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * Sco主机发现程序。
 * 程序返回：
 *   1   主机名
 *   2   OS类型
 *   3   主机型号
 *   4   OS版本
 *   5   内核ID
 *   6   总线类型
 *   7   编号
 *   8   用户许可证数
 *   9   OEM号
 *   a   起源号
 */
public class ScoOS extends ScoFinder {

    private static final Logger logger = Logger.getLogger(ScoOS.class);
    private static final String CMDSTR = "uname -X |grep =";

    /*
     # uname -X
     System = SCO_SV     //系统名称    2 系统类型
     Node = scosysv      //节点名称    1 主机名
     Release = 3.2v5.0.5 //系统版本     3
     KernelID = 98/07/02 //内核ID    5
     Machine = i80386    //主机型号    4
     BusType = ISA       //总线类型     6
     Serial = 2FI083327  //编号               7
     Users = 5-user      //用户许可证数 8
     OEM# = 0            //OEM号                  9
     Origin# = 1         //起源号              10
     NumCPU = 1
     */

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] osRes = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, CMDSTR, 0, 0);
            List<String> strList = new ArrayList<String>();
            while ((strLine = reader.readLine()) != null) {
                strList.add(strLine);
            }
            //            if (strList.size() < 11) {
            //                logger.error("failed to get ScoOS ");
            //                return list;
            //            }
            String ptnStr = "\\s*(.+)\\s*(=)\\s*(.+)";
            Pattern pattern = Pattern.compile(ptnStr);
            osRes = new String[strList.size() + 3];
            osRes[0] = "0";
            osRes[1] = "OS";
            osRes[2] = getHostName(ip) + " " + "OS";
            for (int i = 0; i < strList.size(); i++) {
                Matcher matcher = pattern.matcher(strList.get(i));
                if (matcher.find()) {
                    osRes[3 + i] = matcher.group(3);
                }
            }
            if (osRes.length == 14) {
                //为了返回的数据和数据库里的数据位置正确对应做此操作
                exchange(osRes, 3, 4);
                exchange(osRes, 6, 7);
                String numCPU = osRes[osRes.length - 1];
                for (int i = osRes.length - 1; i > 4; i--) {
                    osRes[i] = osRes[i - 1];
                }
                osRes[4] = numCPU;
                //exchange(osRes,)
                list.add(osRes);
                return list;
            }
        } catch (IOException e) {
            logger.error("failed to get ScoOS ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }

    private void exchange(String[] ss, int index1, int index2) {
        if (ss.length >= index1 && ss.length >= index2) {
            String temp = ss[index1];
            ss[index1] = ss[index2];
            ss[index2] = temp;
        }
    }
}
